<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 *
 * @author RAMIANDRISOA Domoina
 * @tel 261 34 13 306 32
 * @email domoinaalivelo@gmail.com
 */

class Login extends CI_Controller {

	function __construct()
	    {
		parent::__construct();
		$this->load->helper('url');
		$this->load->helper(array('form', 'url'));
		$this->load->library('form_validation');
		$this->load->library('session');
	    }
		
	function admin()
	{		
		$this->form_validation->set_rules('username', 'username', 'required');
		$this->form_validation->set_rules('password', 'password', 'required');
		if($this->session->userdata('logged_in')=='admin'){
			$this->load->view('pageadmin/pageadmin');
		}
		else {
			if ($this->form_validation->run() == FALSE)
			{
				$this->load->view('pageadmin/loginadmin');				
			}
			else
			{	
				$email = $this->input->post('username');
				$mdp = $this->input->post('password');
				$result = $this->MAdmin->verifCompteUser($email,$mdp);
                                
				if ($result!=0 || $_SESSION['logged_in']=="admin"){				
				    $_SESSION['idemail'] = $email;	
				    $_SESSION['logged_in'] = 'admin';	
				    $_SESSION['idid'] = $result;
					$this->load->view('pageadmin/pageadmin');
				}
				else {
					?>
					<script>alert("Erreur login ou mot de passe incorrect");</script>
					<?php 
					$this->load->view('pageadmin/loginadmin');
				}
			}		
		}
	}

}
?>